package com.skycode5.dao;

import com.skycode5.bean.NewsBean;
import com.skycode5.util.DBConnection;
import com.sun.xml.internal.bind.v2.runtime.reflect.Lister;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

public class NewsDao {
    DBConnection DBConn = new DBConnection();
    private int newsID;

    // todo 后期添加分页功能
    public List<NewsBean> getList() {
        Connection conn = DBConn.getConn();
        String sql = "select * from news limit "+(1)*10+",10";
        List<NewsBean> list = new ArrayList<NewsBean>();
        try {
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                NewsBean newsBean = new NewsBean();
                newsBean.setNewsId(rs.getInt("news_id"));
                newsBean.setNewsTitle(rs.getString("news_title"));
                newsBean.setNewsContent(rs.getString("news_content"));
                newsBean.setNewsTime(rs.getString("news_time"));
                newsBean.setAdminName(rs.getString("AdminName"));
                list.add(newsBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public int add(NewsBean news) {
        Connection conn = DBConn.getConn();
        String sql = "insert into news values(null,?,?,?,?)";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, news.getNewsTitle());
            pmst.setString(2, news.getNewsContent());
            pmst.setString(3, (new Date()).toString());
            pmst.setString(4, news.getAdminName());
            System.out.println(pmst.executeUpdate());
            return pmst.executeUpdate();// 执行添加 返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;//添加失败返回0
    }

    public int update(NewsBean news) {
        Connection conn = DBConn.getConn();
        String sql = "update news set NewsTitle=?,NewsContent=?,NewsTime=?,AdminName=? where NewsId=?";
        try {
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setString(1, news.getNewsTitle());
            pmst.setString(2, news.getNewsContent());
            pmst.setString(3, "2024-10-10");
            pmst.setString(4, news.getAdminName());
            pmst.setInt(5, news.getNewsId());
            return pmst.executeUpdate(sql);// 执行添加 返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;//添加失败返回0
    }
    
    public int delete(int NewsId) {
        Connection conn = DBConn.getConn();
        try {
            String sql = "delete from news where NewsId=?";
            PreparedStatement pmst = conn.prepareStatement(sql);
            pmst.setInt(1,newsID);
            return pmst.executeUpdate(sql);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }
    /*
    *@return
    */
    public int count(){
        Connection conn = DBConn.getConn();
        try {
            String sql = "select count(*) from news";
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()){
                return rs.getInt(1);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return 0;
    }
}